import torch
import torchvision

from torch import nn
from torch.nn import Sequential, Conv2d, MaxPool2d, Flatten, Linear, CrossEntropyLoss
from torch.utils.data import DataLoader

dataset = torchvision.datasets.CIFAR10('./dataset', train=False, transform=torchvision.transforms.ToTensor(), download=True)
dataload = DataLoader(dataset, batch_size=64)

class module(nn.Module):

    def __init__(self):

        super(module, self).__init__()
        self.model1 = Sequential(
            Conv2d(3, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 64, 5, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 20)

        )

    def forward(self, x):

        x = self.model1(x)
        return x

mudule_net = module()
loss = CrossEntropyLoss()
for data in dataload:
    imgs, targets = data
    output = mudule_net(imgs)
    result_loss = loss(output, targets)
    result_loss.backward()
